import java.awt.Dimension;
import java.util.Date;

import javax.swing.JSlider;

/*
 * TeaTimer.java
 *
 *Developed: Thomas Georg
 * Created on 28. April 2008, 14:29
 */



/**
 *
 * @author  Tom
 */
public class TeaTimer extends javax.swing.JFrame {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Creates new form TeaTimer */
    public TeaTimer() {
        initComponents();
    }
    
    int zeitwert=60;
    Thread t1;
    
    global_values values = new global_values();
    

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    	
    	t1 =   new Thread( new countdown(values) );
    	t1.start();
    	
        jLabel1 = new javax.swing.JLabel();
        minuteslabel = new javax.swing.JLabel();
        gobutton = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jSlider1 = new javax.swing.JSlider();
        minutesdispay = new javax.swing.JLabel();
        remainingtime_label = new javax.swing.JLabel();
        remaining_time = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        File = new javax.swing.JMenu();
        about = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();
        setTitle("TeaTimer");
        setResizable(false);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(400, 400, 0, 0));
        
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setText("TeaTimer");

        minuteslabel.setText("Minutes:");

        gobutton.setText("Start TeaTimer");
        Dimension minknopfgroesse = new Dimension();
        minknopfgroesse.setSize(150, 1);
        gobutton.setMinimumSize(minknopfgroesse);
        gobutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                gobuttonMousePressed(evt);
            }
        });

        jSlider1.setMaximum(20);
        jSlider1.setMinimum(0);
        jSlider1.setValue(2);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        minutesdispay.setText("1:00");

        remainingtime_label.setText("Remaining Time:");

        remaining_time.setText("0:00");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 8));
        jLabel4.setText(values.getVersion());

        File.setText("File");

        about.setText("About TeaTimer");
        about.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                aboutMousePressed(evt);
            }
        });
        File.add(about);

        exit.setText("Exit");
        exit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                exitMousePressed(evt);
            }
        });
        File.add(exit);

        jMenuBar1.add(File);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(gobutton))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(minuteslabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(minutesdispay))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(remainingtime_label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(remaining_time))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(minutesdispay, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minuteslabel, javax.swing.GroupLayout.Alignment.LEADING))
                .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(gobutton)
                .addGap(18, 18, 18)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(remainingtime_label)
                    .addComponent(remaining_time))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void gobuttonMousePressed(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
    	
    	
    	values.setRemaining_time(remaining_time); 
    	values.setJProgressBar1(jProgressBar1);
    	values.setGobutton(gobutton);
    	
    	values.stop=true;
    	
    	if(values.isRunning())
    	{
    			values.stop=true;
    			t1.interrupt();
    			values.setRunning(false);
    			gobutton.setText("Start TeaTimer");
    			System.out.println("stoped");
    			System.out.println("Start "+values.getListeners().getListenerCount());
    			
    			values.setZeitwert(zeitwert);
    	}
    	else
    	{
    		values.stop=false;

    		System.out.println("Start 2 "+values.getListeners().getListenerCount()+" "+zeitwert);
    		Date datum =new java.util.Date();
    		values.setZeitwert(datum.getTime());
    		
    		
    		values.setDeadline(zeitwert*1000+datum.getTime());
    		values.setStarttime(datum.getTime());
    		
    		values.setRunning(true);
    			
    		
    		values.setDurchlaufszaehler_puls_1();
    		System.out.println("counte started");
    		
    		gobutton.setText("Stop timer");
    		
    	}
    }                                     

    private void aboutMousePressed(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
        About about_window = new About();
        about_window.setVisible(true);
    }                                  

    private void exitMousePressed(java.awt.event.MouseEvent evt) {                                  
        // TODO add your handling code here:
    	System.exit(0);
    }                                 

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
        // TODO add your handling code here:
    	zeitwert=((JSlider) evt.getSource()).getValue();
    	if(zeitwert%2==0)
    	{
    		zeitwert/=2;   		
    		minutesdispay.setText(zeitwert+":00");
    		zeitwert*=60;
    	}
    	else
    	{
    		zeitwert/=2;
    		minutesdispay.setText(zeitwert+":"+30);
    		zeitwert*=60;
    		zeitwert+=30;
    	}
    }                                     
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TeaTimer().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JMenu File;
    private javax.swing.JMenuItem about;
    private javax.swing.JMenuItem exit;
    private javax.swing.JButton gobutton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JLabel minutesdispay;
    private javax.swing.JLabel minuteslabel;
    private javax.swing.JLabel remaining_time;
    private javax.swing.JLabel remainingtime_label;
    // End of variables declaration
    
}
